import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class Solution736 {

    int cur=1;
    Stack<Map<String,Integer>> stack=new Stack<>();

    public int evaluate(String expression) {
        String s=expression.substring(cur,cur+3);
        cur+=4;
        if (s.equals("mul")){
            cur++;
        }
        String last=null;
        Map<String,Integer> map=new HashMap<>();
        stack.push(map);
        Integer a=null,b=null;
        while (expression.charAt(cur)!=')'){
            if (expression.charAt(cur)=='('){
                cur++;
                int m=evaluate(expression);
                if (last==null) {
                    if (a == null) {
                        a = m;
                    } else {
                        b = m;
                    }
                }else {
                    map.put(last,m);
                    last=null;
                }
            }else if (expression.charAt(cur)==' '){
                cur++;
            }else if (expression.charAt(cur)>='0' && expression.charAt(cur)<='9' || expression.charAt(cur)=='-') {
                int t=cur;
                while (expression.charAt(t+1)!=' ' && expression.charAt(t+1)!=')'){
                    t++;
                }
                int m=Integer.parseInt(expression.substring(cur, t + 1));
                if (last==null) {
                    if (a == null) {
                        a = m;
                    } else {
                        b = m;
                    }
                }else {
                    map.put(last,m);
                    last=null;
                }
                cur=t+1;
            }else{
                int t=cur;
                while (expression.charAt(t+1)!=' ' && expression.charAt(t+1)!=')'){
                    t++;
                }
                String tt=expression.substring(cur,t+1);
                cur=t+1;
                if (last!=null){
                    for (int i= stack.size()-1;i>=0;i--){
                        if (stack.get(i).containsKey(tt)){
                            map.put(last,stack.get(i).get(tt));
                            break;
                        }
                    }
                    last=null;
                    continue;
                }
                if (expression.charAt(cur)==' ' && s.equals("let")) {
                    last = tt;
                }else {
                    for (int i= stack.size()-1;i>=0;i--){
                        if (stack.get(i).containsKey(tt)){
                            if (a==null){
                                a=stack.get(i).get(tt);
                            }else {
                                b=stack.get(i).get(tt);
                            }
                            break;
                        }
                    }
                }
            }
        }
        stack.pop();
        cur++;
        if (s.equals("mul")){
            return a*b;
        }else if (s.equals("add")){
            return a+b;
        }else {
            return a;
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution736().evaluate("(let x -2 y x y)"));
    }
}
